package cn.broadin.app.hotel.component;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * 卡片视图
 */
public class CardView extends BaseView<CardViewData>{

    public CardView(Context context, CardViewData data) {
        super(context, data);
    }

    @Override
    public View onCreateView(RelativeLayout rootView) {
        if(mData.isGif != null && mData.isGif){
            SimpleDraweeView mSimpleDraweeView = new SimpleDraweeView(mContext);
            if(TextUtils.isEmpty(mData.icon)){
                Uri uri = new Uri.Builder()
                        .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
                        .path(String.valueOf(mData.defaultResId))
                        .build();
                DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                        .setUri(uri)
                        .setAutoPlayAnimations(true)
                        .build();
                mSimpleDraweeView.setController(draweeController);
            } else {
                DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                        .setUri(Uri.parse(mData.icon))
                        .setAutoPlayAnimations(true)
                        .build();
                mSimpleDraweeView.setController(draweeController);
            }
            return mSimpleDraweeView;
        } else {
            ImageView mImageView = new ImageView(mContext);
            // 加载图标
            if(TextUtils.isEmpty(mData.icon)){
                mImageView.setImageResource(mData.defaultResId);
            } else {
                RequestOptions requestOptions = new RequestOptions()
                        .placeholder(mData.defaultResId)
                        .error(mData.defaultResId)
                        .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .centerCrop();
                Glide.with(getContext()).load(mData.icon).apply(requestOptions).into(mImageView);
            }
            return mImageView;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        jumpToOtherActivity(mData);
    }

    public static CardView newInstance(Context context, JSONObject jsonObject){
        CardViewData cardViewData = JSON.toJavaObject(jsonObject, CardViewData.class);
        return newInstance(context, cardViewData);
    }

    public static CardView newInstance(Context context, CardViewData cardViewData){
        return new CardView(context, cardViewData);
    }
}
